package org.afun.csms_android;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.afun.csms_android.domain.User;

public class BasicMsgActivity extends AppCompatActivity {
    private User user;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_msg);
        Intent intent = getIntent();
        this.user = (User) intent.getSerializableExtra("user");
        this.mContext = this;
        init();
    }

    private void init() {
        TextView accountText = findViewById(R.id.ua);
        TextView pwdText = findViewById(R.id.upwd);
        TextView nameText = findViewById(R.id.un);
        TextView roleText = findViewById(R.id.ur);
        TextView genderText = findViewById(R.id.ug);

        accountText.setText(user.getAccount());
        pwdText.setText(user.getPassword());
        nameText.setText(user.getName());
        String roleName = "";
        switch (user.getRoleId()) {
            case 1:
                roleName = "管理员";
                break;
            case 2:
                roleName = "教师";
                break;
            case 3:
                roleName = "学生";
                break;
            default:
                break;
        }
        roleText.setText(roleName);
        genderText.setText(user.getGender());

        Button selfMsgAlterBtn = findViewById(R.id.selfMsgAlterBtn);
        Button cancelAlterMsg = findViewById(R.id.cancelAlterMsg);
        String finalRoleName = roleName;
        selfMsgAlterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, UserAlterActivity.class);
                intent.putExtra("userId", user.getId());
                intent.putExtra("userAccount", user.getAccount());
                intent.putExtra("userPwd", user.getPassword());
                intent.putExtra("userName", user.getName());
                intent.putExtra("userRole", finalRoleName);
                intent.putExtra("userGender", user.getGender());
                mContext.startActivity(intent);
            }
        });
        cancelAlterMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}